<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// use Illuminate\Support\Facades\Hash;
class UserController extends Controller{
	public function __construct()
    {
        $this->middleware('admin');
    }
    public function users()
    {
        return view('admin.user.users');
    }
    public function udata(Request $request){
        $key=$request->input('key');
        $sex=$request->input('sex');
        if (!empty($request->input('start_time'))) {
            $start_time=strtotime($request->input('start_time'));
        }else{
            $start_time=0;
        }
        if (!empty($request->input('end_time'))) {
            $end_time=strtotime($request->input('end_time'));
        }else{
            $end_time=time();
        }
        if($sex==0){
            if(!empty($key)){
                $count = DB::table('users')->whereRaw("concat(`name`,`nickname`) like '%".$key."%'")->where('time','>',"{$start_time}")->where('time','<',"{$end_time}")->count();
            }else{
                $count = DB::table('users')->where('time','>',"{$start_time}")->where('time','<',"{$end_time}")->count();
            }
        }else{
            if(!empty($key)){
                $count = DB::table('users')->whereRaw("concat(`name`,`nickname`) like '%".$key."%'")->where('time','>',"{$start_time}")->where('time','<',"{$end_time}")->where('sex',$sex)->count();
            }else{
                $count = DB::table('users')->where('time','>',"{$start_time}")->where('time','<',"{$end_time}")->where('sex',$sex)->count();
            }
        }
        $pageNum=$request->input('page');
        $limit=$request->input('limit');
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        if($sex==0){
            if(!empty($key)){
                $users = DB::table('users')->whereRaw("concat(`name`,`nickname`) like '%".$key."%'")->where('time','>',"{$start_time}")->where('time','<',"{$end_time}")->orderBy('id','desc')->offset($page)->take($limit)->get();
            }else{
                $users = DB::table('users')->where('time','>',"{$start_time}")->where('time','<',"{$end_time}")->orderBy('id','desc')->offset($page)->take($limit)->get();
            }
        }else{
            if(!empty($key)){
                $users = DB::table('users')->whereRaw("concat(`name`,`nickname`) like '%".$key."%'")->where('time','>',"{$start_time}")->where('time','<',"{$end_time}")->where('sex',$sex)->orderBy('id','desc')->offset($page)->take($limit)->get();
            }else{
                $users = DB::table('users')->where('time','>',"{$start_time}")->where('time','<',"{$end_time}")->where('sex',$sex)->orderBy('id','desc')->offset($page)->take($limit)->get();
            }
        }
        foreach ($users as $k=>$item){
            $users[$k]->time =date('Y-m-d H:i:s',$users[$k]->time);
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $count,
            'data'  => $users
        ];
        return response()->json($data);
    }
}